package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.CardTypeAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.CardItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.SystemUtils;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * 银行卡类型列表
 * 
 * @author mty
 * 
 */
public class CardTypeActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {
	private final static int SUCCESS = 1;
	private final static int FAILURE = 2;

	private RelativeLayout mReturn;
	private ListView mListView;
	// private List<SupportBank> mList;
	private CardTypeAdapter mAdapternew;
	private ArrayList<CardItem> datalist = new ArrayList<CardItem>();
	private List<CardItem> cardList;
	private String desc;

	Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SUCCESS:
				datalist.addAll(cardList);
				mAdapternew.notifyDataSetChanged();
				break;
			case FAILURE:
				showToast(desc);
				break;
			}
		};

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_card_type);
		initView();
		setListener();
		questCardList();
		// logic();
	}

	public void initView() {
		mReturn = (RelativeLayout) this.findViewById(R.id.add_title_img);
		mListView = (ListView) this.findViewById(R.id.type_list);
		mListView.setDividerHeight(0);
		mAdapternew = new CardTypeAdapter(this, datalist);
		mListView.setAdapter(mAdapternew);
	}

	public void setListener() {
		mReturn.setOnClickListener(this);
		mListView.setOnItemClickListener(this);
	}

	public void questCardList() {
		KkdHttpMagager.getInstance().postCard(new TypeToken<List<CardItem>>() {
		}.getType(), new HttpCallBack() {

			@Override
			public void onSuccess(int statusCode, Object responseBody) {
				cardList = (List<CardItem>) responseBody;
				mHandler.sendEmptyMessage(SUCCESS);
			}

			@Override
			public void onFailure(int statusCode, String desc) {
				CardTypeActivity.this.desc = desc;
				mHandler.sendEmptyMessage(FAILURE);
			}
		});
	}

	// public void logic() {
	// mList = new ArrayList<SupportBank>();
	// mList.add(new SupportBank("中国银行", R.drawable.china_logo));
	// mList.add(new SupportBank("农业银行", R.drawable.agriculture_logo));
	// mList.add(new SupportBank("建设银行", R.drawable.construction_logo));
	// mList.add(new SupportBank("工商银行", R.drawable.commerce_logo));
	// mList.add(new SupportBank("招商银行", R.drawable.attract_logo));
	// mList.add(new SupportBank("中信银行", R.drawable.citic_logo));
	// mList.add(new SupportBank("民生银行", R.drawable.livelihood_logo));
	// mList.add(new SupportBank("广发银行", R.drawable.wide_logo));
	// mList.add(new SupportBank("兴业银行", R.drawable.business_logo));
	// mList.add(new SupportBank("光大银行", R.drawable.brighten_logo));
	// // mList.add(new SupportBank("交通银行", R.drawable.traffic_logo));
	// mList.add(new SupportBank("邮政储蓄银行", R.drawable.postal_logo));
	// // mList.add(new SupportBank("中国邮储银行", R.drawable.postal_logo));
	// mList.add(new SupportBank("浦东发展银行", R.drawable.pudong_logo));
	// mList.add(new SupportBank("北京银行", R.drawable.peking_logo));
	// mList.add(new SupportBank("渤海银行", R.drawable.bohai_logo));
	// mList.add(new SupportBank("南京银行", R.drawable.nanjing_logo));
	// mList.add(new SupportBank("浙商银行", R.drawable.zhejiang_logo));
	// mList.add(new SupportBank("平安银行", R.drawable.safeness_logo));
	// CardTypeAdapter mAdapternew = new CardTypeAdapter(this);
	// mAdapternew.appendData(mList, false);
	// mListView.setAdapter(mAdapternew);
	//
	// }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add_title_img:
			finish();
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		for (int i = 0; i < cardList.size(); i++) {
			cardList.get(i).setClick(false);
		}
		CardItem item = cardList.get(position);
		item.setClick(true);
		mAdapternew.notifyDataSetChanged();
		Intent intent = this.getIntent();
		intent.putExtra("bankname", item.getBankName());
		intent.putExtra("bankimg", item.getImageUrl());
		this.setResult(1, intent);
		finish();
	}
}
